package com.ym.controller.admin;

import com.ym.common.constant.Coder;
import com.ym.common.constant.Constants;
import com.ym.core.annotation.ApiSign;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.shiro.ShiroTokenManager;
import com.ym.core.shiro.ShiroUtil;
import com.ym.core.shiro.token.UserAccToken;
import com.ym.core.shiro.token.UserSmsToken;
import com.ym.core.shiro.token.WxToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 登录相关
 *
 * @author: 李涛
 * @version: 2020年08月17日 16:56
 */

@Api(tags = "api-登录")
@RestController
@RequestMapping(value = "/admin", headers = Constants.Api.VERSION)
@Validated
public class LoginController extends BaseController {
	
	@ApiOperation("微信登录")
	@PostMapping(value = "/wxLogin")
	public APIResponse wxLogin(
			@RequestBody @Valid WxToken wxToken
	) {
		APIResponse success = APIResponse.success();
		Subject subject = SecurityUtils.getSubject();
		subject.login(wxToken);
		ShiroTokenManager.bindUserToken();
		success.put("user", ShiroUtil.getLoginUser());
		success.put("sessionId", ShiroUtil.getSession().getId());
		return success;
	}

	@ApiOperation("账号密码登录")
	@PostMapping(value = "/accPwdLogin")
	public APIResponse accPwdLogin(
			@RequestBody @Valid UserAccToken userAccToken
	) {
		APIResponse success = APIResponse.success();
		Subject subject = SecurityUtils.getSubject();
		subject.login(userAccToken);
		ShiroTokenManager.bindUserToken();
		success.put("user", ShiroUtil.getLoginUser());
		success.put("sessionId", ShiroUtil.getSession().getId());
		return success;
	}

	@ApiOperation("账号验证码登录")
	@PostMapping(value = "/accSmsLogin")
	public APIResponse accSmsLogin(
			@RequestBody @Valid UserSmsToken userSmsToken
	) {
		APIResponse success = APIResponse.success();
		Subject subject = SecurityUtils.getSubject();
		subject.login(userSmsToken);
		ShiroTokenManager.bindUserToken();
		success.put("user", ShiroUtil.getLoginUser());
		success.put("sessionId", ShiroUtil.getSession().getId());
		return success;
	}

	/**
	 * 退出登录
	 *
	 * @return
	 */
	@ApiSign(isSign = false)
	@ApiOperation("退出登录")
	@GetMapping("/logOut")
	public APIResponse logOut() {
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		return successMsg("退出成功");
	}

	/**
	 * 未登录，shiro应重定向到登录界面，此处返回未登录状态信息由前端控制跳转页面
	 *
	 * @return
	 */
	@GetMapping(value = "/unAuth")
	@ResponseBody
	public APIResponse unAuth() {
		return failCode(Coder.Login.NO_LOGIN);
	}
}
